package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.mapper.ItemGroupAdviceMapper;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class ItemGroupAdviceServiceImpl implements IItemGroupAdviceService {

    @Autowired
    private ItemGroupAdviceMapper itemGroupAdviceMapper;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;


    /**
     * 检查物料组是否存在
     *
     * @param site
     * @param itemGroup
     * @return
     */
    public Map<String,Object> existItemGroup( String site, String itemGroup ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ) );
        Map<String,Object> itemGroupMap = itemGroupAdviceMapper.selectItemGroupMap( site, itemGroup );
        Assert.notNull( itemGroupMap, I18nUtil.getI18nText( "common.itemGroupNotExist", new String[]{ itemGroup } ) );

        return itemGroupMap;
    }

    /**
     * 查询物料组成员物料
     *
     * @param site
     * @param itemGroup
     * @return
     */
    public List<Map<String,Object>> findItemGroupMember( String site, String itemGroup ){
        Map<String,Object> _itemGroup = existItemGroup( site, itemGroup );
        List<Map<String,Object>> itemGroupMembers = itemGroupAdviceMapper.selectItemGroupMember((String) _itemGroup.get( "HANDLE" ));
        Assert.notEmpty( itemGroupMembers, I18nUtil.getI18nText( "common.itemGroupMember", new String[]{ itemGroup } ) );
        return itemGroupMembers;
    }

    /**
     * 查询机种信息
     *
     * @param site
     * @return
     */
    public Map<String,Object> findItemGroupMapBySite( String site ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        String itemGroup = systemRuleAdviceService.existSystemRule( site, Constants.SYSTEM_RULE_ITEM_GROUP );
        Map<String,Object> itemGroupMap = itemGroupAdviceMapper.selectItemGroupMap( site, itemGroup );
        Assert.notNull( itemGroupMap, I18nUtil.getI18nText( "common.itemGroupNotExist", new String[]{ itemGroup } ) );

        return itemGroupMap;
    }

    /**
     * 查询机种信息
     *
     * @param site
     * @return
     */
    public String findItemGroupBySite( String site ){
        Map<String,Object> itemGroupMap = findItemGroupMapBySite( site );
        return (String) itemGroupMap.get( "ITEM_GROUP" );
    }

    /**
     * 查询物料机种信息
     *
     * @param itemBo
     * @return
     */
    public String findItemGroupByItem( String itemBo ){
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        List<Map<String,String>> itemGroupList = itemGroupAdviceMapper.selectItemGroupByItem( itemBo );
        if( itemGroupList.size() == 0 ){
            return null;
        }else{
            return BOHandleUtil.splitItemGroupHandle( itemGroupList.get( 0 ).get( "ITEM_GROUP_BO" ) )[1];
        }
    }

    /**
     * 查询机种列表
     *
     * @param site
     * @return
     */
    public List<Map<String,Object>> selectItemGroupMapList( String site ){

        return itemGroupAdviceMapper.selectItemGroupMapList( site );

    }
}
